package com.fs.groupSeven.controller;

import com.fs.groupSeven.pojo.Class;
import com.fs.groupSeven.pojo.College;
import com.fs.groupSeven.service.ClassService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/class")
public class ClassController {

    @Autowired
    private ClassService classService;

//    根据id找到相应的的班级
    @RequestMapping("/classView")
    public String classView(Integer collegeId){
        return "classManageYRZ/classManage";
    }
//    得到相应的班级数据以及分页
    @RequestMapping("/getlistByPage")
    @ResponseBody
    public Map<String,Object> getlistByPage(Integer currentPage, Integer pageSize,Integer collegeId){
        PageInfo<Class> pageInfo = classService.queryclassAll(currentPage, pageSize,collegeId);
        System.out.println(pageInfo.getList());
        Map<String, Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取班级数据成功");
        result.put("classList",pageInfo.getList());// 返回查询出来的分页数据
        result.put("totalPage",pageInfo.getPages());//返回当前总共有多少页
        return result;
    }
//根据id获取某一个班级的信息
    @RequestMapping("/gettow")
    @ResponseBody
    public Map<String,Object> getont(Integer classId){
        Map<String,Object> result = new HashMap<>();
        Class aClass = classService.queryclassbyId(classId);
        if (aClass == null){
            result.put("code",-1);
            result.put("message","获取班级数据失败");
        }else {
            result.put("code",0);
            result.put("message","获取班级数据成功");
            result.put("classone",aClass);
        }
        return result;
    }
//    编辑对象页面
    @RequestMapping("/edit")
    @ResponseBody
    public Map<String,Object> edit(Class clas){
        Map<String,Object> result = new HashMap<>();
        boolean modified;
        if (clas.getClassId()>0){
            modified = classService.updateOne(clas);
        }else {
            modified = classService.addTwo(clas);
        }
        if (!modified){
            result.put("code",-1);
            result.put("message","编辑班级数据失败");
        }else {
            result.put("code",0);
            result.put("message","编辑班级数据成功");
        }
        return result;
    }
//删除对象
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> detele(Integer classId){
        Map<String,Object> result = new HashMap<>();
        boolean delete = classService.deleteOne(classId);

        if (!delete){
            result.put("code",-1);
            result.put("message","删除班级数据失败");
        }else {
            result.put("code",0);
            result.put("message","删除班级数据成功");
        }
        return result;
    }

//    模糊查询
    @RequestMapping("/selectone")
    @ResponseBody
    public Map<String,Object> selectone(String classname){
        Map<String,Object> result = new HashMap<>();
        Set<Class> selectone = classService.getUserThatAddressLike(classname);

        if (selectone==null){
            result.put("code",-1);
            result.put("message","模糊查询失败");
        }else {
            result.put("code",0);
            result.put("selectdd",selectone);
            result.put("message","模糊查询成功");
        }
        return result;
    }
}
